package com.portal.controller;

import com.portal.common.Result;
import com.portal.entity.Carousel;
import com.portal.entity.Solution;
import com.portal.entity.Case;
import com.portal.service.CarouselService;
import com.portal.service.SolutionService;
import com.portal.service.CaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 首页控制器
 */
@RestController
@RequestMapping("/home")
public class HomeController {
    
    @Autowired
    private CarouselService carouselService;
    
    @Autowired
    private SolutionService solutionService;
    
    @Autowired
    private CaseService caseService;
    
    /**
     * 获取首页数据
     */
    @GetMapping
    public Result<Map<String, Object>> getHomeData() {
        try {
            Map<String, Object> data = new HashMap<>();
            
            // 获取轮播图列表
            List<Carousel> carousels = carouselService.getEnabledCarousels();
            data.put("carousels", carousels);
            
            // 获取解决方案列表
            List<Solution> solutions = solutionService.getEnabledSolutions();
            data.put("solutions", solutions);
            
            // 获取推荐案例列表
            List<Case> cases = caseService.getRecommendCases(3);
            data.put("cases", cases);
            
            return Result.success(data);
        } catch (Exception e) {
            return Result.error("获取首页数据失败：" + e.getMessage());
        }
    }

    /**
     * 获取轮播图列表
     */
    @GetMapping("/banner")
    public Result<List<Carousel>> getBannerList() {
        try {
            List<Carousel> carousels = carouselService.getEnabledCarousels();
            return Result.success(carousels);
        } catch (Exception e) {
            return Result.error("获取轮播图列表失败：" + e.getMessage());
        }
    }

    /**
     * 获取精选案例列表
     */
    @GetMapping("/cases")
    public Result<List<Case>> getFeaturedCases() {
        try {
            List<Case> cases = caseService.getRecommendCases(6);
            return Result.success(cases);
        } catch (Exception e) {
            return Result.error("获取精选案例失败：" + e.getMessage());
        }
    }

    /**
     * 获取解决方案列表
     */
    @GetMapping("/solutions")
    public Result<List<Solution>> getSolutionList() {
        try {
            List<Solution> solutions = solutionService.getEnabledSolutions();
            return Result.success(solutions);
        } catch (Exception e) {
            return Result.error("获取解决方案列表失败：" + e.getMessage());
        }
    }
} 